const {
  $util,
} = getApp();
Page({
  data: {
    list: [],
    page: 1,
    isTrue: false,
    isloadmore: true,
    keyword: '',
    nav: [{
      id: 0,
      title: '待申报',
    }, {
      id: 1,
      title: '已申报',
    }],
    cateIndex: 0,
    yearIndex: 0,
    cycleIndex: 0,
    showFilter: false,
    imgUrl: $util.getImgUrl(),
  },
  onShow() {
    this.loadCondition();
  },
  changeNav(e) {
    this.setData({
      page: 1,
      cateIndex: e.detail.obj.id,
      list: [],
      bannerList: [],
      keyword: '', // 清空搜索关键词
      isLoad: false,
    }, () => {
      this.loadList();
    });
  },
  bindShowFilter() {
    this.setData({
      showFilter: !this.data.showFilter,
    });
  },
  changeFilterIndex(e) {
    let { id, idx } = e.currentTarget.dataset;
    this.setData({
      [id]: idx,
      page: 1,
    });
  },
  sureFilter() {
    this.loadList();
    this.bindShowFilter();
  },
  loadCondition() {
    $util.request({
      url: 'entry/wxapp/partywork.appraisal.getcondition',
      data: {},
      showLoading: false,
      success: (res) => {
        this.setData({
          cycletype: res.cycletype,
          raisalyear: res.raisalyear,
        });
        this.setData({
          page: 1,
          isLoad: false,
        }, () => {
          this.loadList();
        });
      },
    });
  },
  loadList() {
    let {
      page,
      yearIndex,
      cycleIndex,
      keyword,
      cateIndex,
      cycletype,
      raisalyear,
    } = this.data;
    let param = {
      page: page,
      raisalyear: raisalyear[yearIndex],
      cycletype: cycletype[cycleIndex].id,
      type: cateIndex,
      keyword: keyword,
    };
    $util.request({
      url: 'entry/wxapp/partywork.appraisal.getlist',
      data: param,
      showLoading: true,
      success: (res) => {
        let list = res.list;
        if (page > 1) {
          list = [...this.data.list, ...list];
        }
        this.setData({
          list,
          isloadmore: res.list.length > 0 ? true : false,
          page: res.list.length > 0 ? page + 1 : page,
          isLoad: true,
        });
      },
    });
  },
  bindIpVal(e) {
    this.setData({
      keyword: e.detail.value,
    });
  },
  search() {
      this.setData({
        page: 1,
      }, () => {
        this.loadList();
      });
  },
  onReachBottom() {
    this.data.isloadmore && this.loadList();
  },
});